import { useState } from 'react';

import { makeFacade, AuthFacade } from '@/facade/authFacade';
import { AccessItem, Org } from '@/models/Auth';
import { useObservable } from '@/utils/rxHelpers';

export type AuthHookTuple = [AuthFacade, boolean, string, string, Org[], Taro.UserInfo, number, number, string, AccessItem[], string, number];

//需要在页面用到的变量都在这里hook，主要用于简化页面调用逻辑和代码
export function useAuthFacade(): AuthHookTuple {
  const [authFacade] = useState(() => makeFacade());
  //useObservable可以订阅，获取和取消订阅Rxjs的流
  const [loginStatus] = useObservable(authFacade.loginStatus$, false);
  const [realName] = useObservable(authFacade.realName$, "");
  const [orgs] = useObservable(authFacade.orgs$, []);
  const [userId] = useObservable(authFacade.userId$, '');
  const [userInfo] = useObservable(authFacade.userInfo$, {} as Taro.UserInfo);
  const [userApproveStatus] = useObservable(authFacade.userApproveStatus$, 0);
  const [orgApproveStatus] = useObservable(authFacade.orgApproveStatus$, 0);
  const [idCardNo] = useObservable(authFacade.idCardNo$, "");
  const [subscription] = useObservable(authFacade.subscription$, []);
  const [desc] = useObservable(authFacade.desc$, '')
  const [sexual] = useObservable(authFacade.sexual$, 0)

  return [authFacade, loginStatus, userId, realName, orgs, userInfo, userApproveStatus, orgApproveStatus, idCardNo, subscription, desc, sexual];
}
